package com.nlscan.ncomgateway.core.newland.helper

import android.content.Context
import com.nlscan.android.scan.ScanManager

/**
 * Nls 系统 Api support helper
 * ScanManager 辅助类
 * -- 迁移自 ScanSettings#com/nlscan/android/scansettings/misc/Constants.java
 * -- 判读当前固件系统jar是否支持指定API
 * -- 未来考虑 System API 扩展，转化
 */
object NlsSystemApiHelper {


    //------------------ ScanManager 相关 BEGIN -------------------------/

    fun isSupportSetTriggerFunction(): Boolean {
        try {
            val cls: Class<*> = ScanManager::class.java
            val setTriggerFunction = cls.getMethod(
                "setTriggerFunction",
                String::class.java,
                String::class.java
            )
            return setTriggerFunction != null
        } catch (e: Exception) {
        }
        return false
    }

    fun isSupportAddTriggerFunction(): Boolean {
        try {
            val cls: Class<*> = ScanManager::class.java
            val setTriggerFunction = cls.getMethod(
                "addTriggerFunction",
                String::class.java,
                String::class.java,
                String::class.java
            )
            return setTriggerFunction != null
        } catch (e: Exception) {
        }
        return false
    }

    fun isSupportProfile(): Boolean {
        try {
            val cls: Class<*> = ScanManager::class.java
            val setOutputBroadcastParamsOfProfile = cls.getMethod(
                "setOutputBroadcastParamsOfProfile",
                String::class.java,
                String::class.java,
                String::class.java,
                String::class.java,
                String::class.java
            )
            return setOutputBroadcastParamsOfProfile != null
        } catch (e: Exception) {
        }
        return false
    }

    /**
     * 1D头
     * @return
     */
    fun is1DScanner(): Boolean {
        val scanManager = ScanManager.getInstance()
        return scanManager.scannerType != 2
    }

    fun isProfileManagerInstall(context: Context): Boolean {
        try {
            val pm = context.packageManager
            val app = pm.getApplicationInfo("com.nlscan.profilesettings", 0)
            return app != null
        } catch (e: Exception) {
        }
        return false
    }

    /**
     * 直接输出（直接填充，模拟键输出）时同时输出广播
     */
    fun isSupportSendBroadcastOnDirect(): Boolean {
        try {
            val cls = Class.forName("com.nlscan.android.scan.ScanManager")
            val method = cls.getDeclaredMethod(
                "setSendBroadcastOnDirect",
                String::class.java, java.lang.Boolean.TYPE
            )
            return method != null
        } catch (e: Exception) {
            //e.printStackTrace();
        }
        return false
    }

    fun isSupportBatchCommit(): Boolean {
        try {
            val cls: Class<*> = ScanManager::class.java
            val beginBatchSetting = cls.getMethod("beginBatchSetting", java.lang.Long.TYPE)
            return beginBatchSetting != null
        } catch (e: Exception) {
        }
        return false
    }


    //------------------ ScanManager 相关 END -------------------------/


}